<?php
require_once("includes/database.php");

class NguoiDung extends database {
	protected $maSo;
	protected $matKhau;
	protected $ho;
	protected $ten;
	protected $ngaySinh;
	protected $phai;
	protected $email;
	protected $prefix; // Sinh vien = 'SV_' | Co van hoc tap = 'CVHT_' | Hoi dong khoa = 'HDK_'
	protected $table;
	
	public function __construct() {
		parent::__construct();
	}
	
	public function setMaSo($maSo) { $this->maSo = $maSo; }
	public function getMaSo() { return $this->maSo; }
	
	public function setMatKhau($matKhau) { $this->matKhau = $matKhau; }
	public function getMatKhau() { return $this->matKhau; }
	
	public function setHo($ho) { $this->ho = $ho; }
	public function setTen($ten) { $this->ten = $ten; }
	
	public function getHoTen() { return $this->ho . " " . $this->ten; }
	
	public function setNgaySinh($ngaySinh) { $this->ngaySinh = $ngaySinh; }
	public function getNgaySinh() {	return $this->ngaySinh; }
	
	public function setPhai($phai) { $this->phai = $phai; }
	public function getPhai() { return ($this->phai ? "Nam" : "Nữ"); }
	
	public function setEmail($email) { $this->email = $email; }
	public function getEmail() { return $this->email; }
	
	public function getPrefix() { return $this->prefix; }
	
	public function themNguoiDung($extra = "") {
		$data = array(
					$this->prefix . "MA_SO" => $this->maSo,
					$this->prefix . "MAT_KHAU" => md5($this->maSo),
					$this->prefix . "HO" => $this->ho,
					$this->prefix . "TEN" => $this->ten,
					$this->prefix . "NGAY_SINH" => $this->ngaySinh,
					$this->prefix . "PHAI" => $this->phai,
					$this->prefix . "EMAIL" => $this->email
					);
					
		if (!empty($extra) && is_array($extra))
			foreach ($extra as $key => $val)
				$data[$key] = $val;
		
		return $this->insert($this->table, $data);
	}
	
	public function suaNguoiDung($extra = "") {
		$data = array(
					$this->prefix . "HO" => $this->ho,
					$this->prefix . "TEN" => $this->ten,
					$this->prefix . "NGAY_SINH" => $this->ngaySinh,
					$this->prefix . "PHAI" => $this->phai,
					$this->prefix . "EMAIL" => $this->email
					);
					
		if (!empty($extra) && is_array($extra))
			foreach ($extra as $key => $val)
				$data[$key] = $val;
		
		return $this->update($this->table, $data, $this->prefix . "MA_SO = " . $this->maSo);
	}
	
	public function xoaNguoiDung() {
		return $this->delete($this->table, $this->prefix . "MA_SO = " . $this->maSo);
	}
	
	public function kiemTra() {
		return $this->isExists($this->table, $this->prefix . "MA_SO = " . $this->maSo . " AND "
							   . $this->prefix . "MAT_KHAU = '" . $this->matKhau . "'");
	}
	
	public function layThongTinNguoiDung() {}
	
	public function reConnectDatabase() {
		$this->__construct();
	}
} // end of class
?>
